package com.ztgf.sys.api.cms;

import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.bo.HeadParamsBO;
import com.ztgf.common.constant.CommonParameter;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.sys.models.bo.SysEmailManageBO;
import com.ztgf.sys.models.bo.SysEmailManageSaveBO;
import com.ztgf.sys.models.service.ISysEmailManageService;
import com.ztgf.sys.models.vo.SysEmailManageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * author: chenyu.wang
 * date: 2020-07-10 15:43
 * Description: 邮件发布管理表控制器
 */
@RestController
@RequestMapping("/cms/sysEmailManage")
@Slf4j
@Api(value = "SysEmailManageController", tags = "邮件发布管理表接口")
public class SysEmailManageController {

    @DubboReference
    private ISysEmailManageService sysEmailManageService;



    @ApiOperation(value = "批量发送邮件",notes = "批量发送邮件")
    @PostMapping(value = "/saveBatchByType")
    public Result<?> saveBatchByType(SysEmailManageSaveBO bo) throws Exception{
        if(bo != null){
            HeadParamsBO headParamsBO = CommonParameter.getHeadParamsBO();
            sysEmailManageService.saveBatchByType(bo,headParamsBO.getSessionId(),headParamsBO.getSource().toString());
            return new Result<>();
        }else {
            return new Result<> (CodeEnum.FAIL_100002);
        }
    }

    @ApiOperation(value = "邮件详情",notes = "邮件详情")
    @PostMapping("/queryPageListByNoticeId")
    public Result<Page<SysEmailManageVO>> queryPageListByNoticeId(SysEmailManageBO sysEmailManageBO){
        if (sysEmailManageBO != null){
            Page<SysEmailManageVO> page = sysEmailManageService.queryPageListByNoticeId(sysEmailManageBO);
            return new Result<>(page);
        }else {
        return new Result<> (CodeEnum.FAIL_100002);
        }
    }

    @PostMapping(value = "/insert")
    @ApiOperation(value = "发布邮件", notes = "发布邮件")
    public Result insert(SysEmailManageSaveBO saveBO) {
        boolean flag = sysEmailManageService.add(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping(value = "/queryAdminEmailList")
    @ApiOperation(value = "邮件管理列表", notes = "邮件管理列表")
    public Result queryAdminEmailList(SysEmailManageBO bo) {
        return new Result<>(sysEmailManageService.queryAdminEmailList(bo));
    }

    @PostMapping(value = "/queryById")
    @ApiOperation(value = "查看发布邮件详情", notes = "查看发布邮件详情")
    public Result queryById(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        return new Result<>(sysEmailManageService.queryById(id));
    }

    @PostMapping(value = "/deleteById")
    @ApiOperation(value = "删除邮件", notes = "删除邮件")
    public Result deleteById(Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        } else {
            boolean flag = sysEmailManageService.deleteById(id);
            if (flag) {
                return new Result<>(CodeEnum.SUCCESS);
            } else {
                return new Result<>(CodeEnum.FAIL_100009);
            }
        }
    }

    @PostMapping(value = "/deleteBatch")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    public Result deleteBatch(String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] split = ids.split(",");
            boolean flag = sysEmailManageService.deleteBatchIds(Arrays.asList(split));
            if (!flag) {
                return new Result<>(CodeEnum.FAIL_100009);
            }
            return new Result<>(CodeEnum.SUCCESS);
        } else {
            return new Result<>(CodeEnum.FAIL_100002);
        }
    }
}
